package cn.spider.visual;

import cn.spider.pojo.Rpt1Vo;
import cn.spider.pojo.Rpt2Vo;
import cn.spider.pojo.Rpt3Vo;
import cn.spider.service.ReportServiceImpl;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * Created by laoYang on 2018/4/22.
 */
@WebServlet(name = "postBar", urlPatterns = {"/postbar.a"})
public class PostBar extends HttpServlet {

    private ReportServiceImpl reportService = new ReportServiceImpl();

    private static List<String> topTitle = null;

    @Override
    public void init() throws ServletException {
        System.out.println("init ....");
        topTitle = reportService.topCategory();
        System.out.println("topTitle = " + topTitle.size());
    }



    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String action = req.getParameter("m");
        if ("r4".equals(action)) r4Report(resp);
        else if ("r2".equals(action)) r2Report(resp);
        else if ("r3".equals(action)) r3Report(resp);
        else if ("r1".equals(action)) r1Report(resp);
        else if ("dict".equals(action)) getdict(resp);

    }

    /**
     * 一级分类字典数据
     */
    private void getdict(HttpServletResponse resp) throws IOException {
        // 字典数据
        if (topTitle == null) {
            topTitle = reportService.topCategory();
        }
        send2View(resp, JSONObject.toJSONString(topTitle));
    }


    /**
     * 最近30天的采集数据量 - 页面中的第四个报表
     */
    private void r4Report(HttpServletResponse resp) throws IOException {
        List<List<Object>> list = reportService.reportLatelyN();
        send2View(resp,JSONObject.toJSONString(list));
    }

    /**
     * 上周数据采集情况 -- 页面中的第三个报表
     */
    private void r3Report(HttpServletResponse resp) throws IOException{
        List<Rpt3Vo> rpt3Vos = reportService.reportLastWeek();
        send2View(resp, JSONObject.toJSONString(rpt3Vos));
    }

    /**
     * 本周数据采集情况 -- 页面中的第二个报表
     */
    private void r2Report(HttpServletResponse resp) throws IOException{
        List<Rpt2Vo> rpt2Vos = reportService.reportCurrentWeek();
        send2View(resp, JSONObject.toJSONString(rpt2Vos));
    }

    /**
     * 历史当前一级分类采集的数据量 -- 页面中的第一个报表
     */
    private void r1Report(HttpServletResponse resp) throws IOException{
        List<Rpt1Vo> list = reportService.reportAll();
        send2View(resp,JSONObject.toJSONString(list));
    }



    /**
     * 将数据发送到页面
     */
    private void send2View(HttpServletResponse resp, String result) throws IOException {
        PrintWriter writer = resp.getWriter();
        writer.write(result);
        writer.flush();
        writer.close();
    }
}
